package com.akcome.apollo.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.map.HashedMap;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.akcome.apollo.common.MathUtil;
import com.akcome.apollo.domain.VStatsPoolHour;
import com.akcome.apollo.dto.PoolSpeedBean;
import com.akcome.apollo.repository.VStatsPoolHourRepository;
import com.akcome.apollo.service.VFoundBlocksService;
import com.akcome.apollo.service.VMiningWorkersService;
import com.akcome.apollo.service.VStatsPoolHourService;


/**
 * VIEWService接口实现
 *
 * @author wn
 * @version $v: 1.0.0, $time:2017-12-26 08:49:00 Exp $
 */
@Service
public class VStatsPoolHourServiceImpl implements VStatsPoolHourService {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private VStatsPoolHourRepository vStatsPoolHourRepository;
	
	@Autowired
	private VMiningWorkersService vMiningWorkersService;
	
	@Autowired
	private VFoundBlocksService vFoundBlocksService;
	
	@Override
	public VStatsPoolHour save(VStatsPoolHour vStatsPoolHour){
		return vStatsPoolHourRepository.save(vStatsPoolHour);
	}
	
	@Override
	public VStatsPoolHour findOne(Long id){
		return vStatsPoolHourRepository.findOne(id);
	}
	
	@Override
	public List<VStatsPoolHour> findIndex(String coinType,String beginDate,String endDate){
		return vStatsPoolHourRepository.findIndex(coinType, beginDate, endDate);
	}
	
	/**
	 * 矿池统计数据 --首页展示
	 * 默认3天的数据
	 * @param coinType
	 * @return
	 */
	@Override
	public Map<String, Object> findIndex(String coinType){
		
		
		Date endDate = new Date();
		SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHH");
		Date beginDate = DateUtils.addDays(endDate, -3);
		
		Date tempDate = beginDate;
		//建立数据集合
		List<Object[]> data = new ArrayList<>();
		do {
			tempDate = DateUtils.addHours(tempDate, 1);
			data.add(new Object[]{sf.format(tempDate),0D});
			
		} while (tempDate.getTime()<endDate.getTime());
		
		
		//数据集合赋值
		List<VStatsPoolHour> vStatsPoolHours = findIndex(coinType, sf.format(beginDate), sf.format(endDate));
		String endDateStr = sf.format(endDate);
		for (VStatsPoolHour vStatsPoolHour : vStatsPoolHours) {
			Long hour = vStatsPoolHour.getHour();
			for (int i=0;i<data.size();i++) {
				Object[] obj = data.get(i);
				if(String.valueOf(obj[0]).equals(String.valueOf(hour))){
					if(endDateStr.equals(""+vStatsPoolHour.getHour())){
						//分钟差
						int minutes =  Integer.parseInt(new SimpleDateFormat("m").format(endDate));
						if(0==minutes){
							//如果是整点则取上一维度的算力
							obj[1]=	data.get(i-1)[1];
						}else{
							obj[1] = MathUtil.div(MathUtil.mul(vStatsPoolHour.getAccept().doubleValue(), Math.pow(256, 4)), MathUtil.mul(Math.pow(10, 12),60*minutes*1000), 4); 
						}
					}else{
						obj[1] = MathUtil.div(MathUtil.mul(vStatsPoolHour.getAccept().doubleValue(), Math.pow(256, 4)), MathUtil.mul(Math.pow(10, 12),60*60*1000), 4); 
					}
				}
			}
		}
		Map<String, Object> result = new HashMap<>();
		result.put("Hs", data);
		return result;
	}
	
	@Override
	public Map<String, Object> indexDate(){
		Map<String, Object> modelMap = new HashedMap<>();
		List<Object> actives = vMiningWorkersService.countByActive();
		for (Object object : actives) {
			Object[] obj = (Object[])object;
			modelMap.put("actice_"+obj[0], obj[1]);
		}
		List<Object> blocks = vFoundBlocksService.findGroupCoinType();
		for (Object object : blocks) {
			Object[] obj = (Object[])object;
			modelMap.put("block_count_"+obj[0], obj[1]);
			modelMap.put("block_earn_"+obj[0], MathUtil.div(Double.parseDouble(String.valueOf(obj[2])), 100000000, 8));
		}
		return modelMap;
	}
}
